//
//  VisitorLoginView.swift
//  WeiBoSwift
//
//  Created by rong on 15/6/24.
//  Copyright © 2015年 rong. All rights reserved.
//

import UIKit
import FFAutoLayout

//view只显示界面，不能做具体的监听后的事情，所以在这里用代理来做
protocol VisitorLoginViewDelegate : NSObjectProtocol{

    func visitorRegisterButtonClicked()
    func visitorLoginButtonClicked()
}

class VisitorLoginView: UIView {
    
    //代理变量  在oc中， weak id <VisitorLoginViewDelegate> delegate
    weak var delegate: VisitorLoginViewDelegate?
    
    // 设置视图内容
    func setupViewInfo(iconName: String, introduce: String, isHome: Bool)
    {
        introduceLabel.text = introduce;
        
        if isHome {
        
            houseIconView.hidden = false;
            iconView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
            
        } else {
            houseIconView.hidden = true;
            iconView.image = UIImage(named: iconName)
            
            //将视图层次置后
            sendSubviewToBack(maskIconView)
            
        }
    }

    //视图默认的构造方法
    override init(frame:CGRect)
    {
        super.init(frame: frame)
        
        setupSubviewUI()
    }
    
    //解档，
    required init(coder aDecoder: NSCoder)
    {
       // print(__FUNCTION__)
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupSubviewUI()
    {
        //0.添加子控件 -- 按照先后顺序，层次分明
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(maskIconView)
        addSubview(houseIconView)
        addSubview(introduceLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        //子控件布局
        autoLayoutSubviewUI()
    }
    
    //给子控件 纯代码设置自动布局
    private func autoLayoutSubviewUI()
    {
        //圆的图标
        iconView.ff_AlignInner(ff_AlignType.CenterCenter, referView: self, size: nil, offset: CGPointMake(0, -40))
        
        //遮罩图片
        maskIconView.ff_AlignVertical(ff_AlignType.CenterCenter, referView: iconView, size: nil)
        
        //房子图片
        houseIconView.ff_AlignVertical(ff_AlignType.CenterCenter, referView: iconView, size: nil)
        
        //文字
        introduceLabel.ff_AlignVertical(ff_AlignType.BottomCenter, referView: iconView, size: CGSizeMake(240, 40), offset: CGPointMake(0, 10))
        
        //注册按钮
        registerBtn.ff_AlignVertical(ff_AlignType.BottomLeft, referView: introduceLabel, size: CGSizeMake(100, 40), offset: CGPointMake(0, 20))
        
        //登录按钮
        loginBtn.ff_AlignVertical(ff_AlignType.BottomRight, referView: introduceLabel, size: CGSizeMake(100, 40), offset: CGPointMake(0, 20))
        
        //设置view的背景颜色
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0);
        
    }
    
    //启动旋转动画
    func startAnimation()
    {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        
        iconView.layer.addAnimation(anim, forKey: nil);
    }

    
    // 懒加载控件
    
    //图标
    lazy var iconView: UIImageView = {
    
        let image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        return UIImageView(image: image)
    
    }()
    
    //遮罩图片
    lazy var maskIconView: UIImageView = {
    
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return UIImageView(image: image)
    }()
    
    //房子
    lazy var houseIconView: UIImageView = {
    
        let image = UIImage(named: "visitordiscover_feed_image_house")
        
        return UIImageView(image: image)
    }()
    
    //文字标签
    lazy var introduceLabel: UILabel = {
    
        let textLabel = UILabel(frame: CGRectMake(0, 0, 240, 40))
        textLabel.text = "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过"
        textLabel.textColor = UIColor.darkGrayColor()
        textLabel.font = UIFont.systemFontOfSize(14.0)
        textLabel.textAlignment = NSTextAlignment.Center
        textLabel.preferredMaxLayoutWidth = 224
        //设定最多两行，超出以后 显示 ...
        textLabel.numberOfLines = 2;
        
        textLabel.sizeToFit()
        
        return textLabel
    }()
    
    //注册按钮
    lazy var registerBtn: UIButton = {
    
        let btn = UIButton(frame: CGRectMake(0, 0, 100, 40))
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //添加“注册”点击事件
        btn.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    //登录按钮
    lazy var loginBtn: UIButton = {
    
        let btn = UIButton(frame: CGRectMake(200, 220, 100, 40))
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //添加“登录”点击事件
        btn.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        return btn

    }()
    
    
    //注册
    func register()
    {
        delegate?.visitorRegisterButtonClicked();
    }
    
    //登录
    func login()
    {
        delegate?.visitorLoginButtonClicked()
    }

}
